package handler

import (
	"github.com/gin-gonic/gin"
	"onerk4/zk2/api/client"
	"onerk4/zk2/api/enit"
	user "onerk4/zk2/common/proto"
)

func UserLogin(c *gin.Context) {
	var req enit.UserLogin

	if err := c.ShouldBind(&req); err != nil {
		enit.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.UserLogin(c, &user.UserLoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
		Sms:      req.Sms,
	})
	if err != nil {
		enit.ErrorResponse(c, "err", err.Error())
		return
	}
	enit.SuccessResponse(c, "ok", login)
}

func SendSms(c *gin.Context) {
	var req enit.SendSmsRequest

	if err := c.ShouldBind(&req); err != nil {
		enit.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	sms, err := client.SendSms(c, &user.SendSmsRequest{
		Mobile: req.Mobile,
		Source: req.Source,
	})
	if err != nil {
		enit.ErrorResponse(c, "err", err.Error())
		return
	}

	enit.SuccessResponse(c, "ok", sms)
}

func SearchEnterpriseData(c *gin.Context) {
	var req enit.SearchEnterpriseData

	if err := c.ShouldBind(&req); err != nil {
		enit.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	userId := c.GetUint("userId")

	sms, err := client.SearchEnterpriseData(c, &user.SearchEnterpriseDataRequest{
		Uid:       int64(userId),
		StoreName: req.StoreName,
	})
	if err != nil {
		enit.ErrorResponse(c, "err", err.Error())
		return
	}

	enit.SuccessResponse(c, "ok", sms)
}
